package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.GeoreMachineDao;
import com.tbit.main.pojo.GeoreMachine;
import com.tbit.main.pojo.MachineAbnormal;
import com.tbit.main.pojo.MachineStatus;
import com.tbit.main.service.GeoreMachineService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2021-06-29 14:38
 */
@Service("georeMachineService")
public class GeoreMachineServiceImpl implements GeoreMachineService {

    @Autowired
    private GeoreMachineDao georeMachineDao;

    @Override
    public GeoreMachine getByMachineId(Integer machineId) {
        return georeMachineDao.getByMachineId(machineId);
    }

    @Override
    public List<GeoreMachine> getByMachineIds(List<Integer> machineIds) {

        List<GeoreMachine> geoReMachines = new LinkedList<>();
        List<List<Integer>> georeMachineS = ListUtils.partition(machineIds, 900);
        for (List<Integer> georeMachines : georeMachineS) {
            geoReMachines.addAll(georeMachineDao.getByMachineIds(StringUtil.getListString(georeMachines)));
        }

        return geoReMachines;
    }

    @Override
    public void updateBatch(List<GeoreMachine> georeMachinesS) {
        List<List<GeoreMachine>> georeMachineS = ListUtils.partition(georeMachinesS, 900);
        for (List<GeoreMachine> georeMachines : georeMachineS) {
            georeMachineDao.updateBatch(georeMachines);
        }
    }

    @Override
    public void insertBatch(List<GeoreMachine> georeMachinesS) {

        List<List<GeoreMachine>> georeMachineS = ListUtils.partition(georeMachinesS, 900);
        for (List<GeoreMachine> georeMachines : georeMachineS) {
            georeMachineDao.insertBatch(georeMachines);
        }
    }

    @Override
    public List<Map<String, Object>> intoMachine(List<Integer> regionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", StringUtil.getListString(regionId));
        map.put(DaoParamConstant.startTime, DateTimeUtil.getDayAgoTime(1));
        return georeMachineDao.intoMachine(map);
    }

    @Override
    public List<Map<String, Object>> outMachine(List<Integer> regionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", StringUtil.getListString(regionId));
        map.put(DaoParamConstant.startTime, DateTimeUtil.getDayAgoTime(1));
        return georeMachineDao.outMachine(map);
    }

    @Override
    public List<Map<String, Object>> getMachine(List<Integer> regionId) {
        return georeMachineDao.getMachine(StringUtil.getListString(regionId));
    }

    @Override
    public Integer getRegionIdMachine(Integer regionId) {
        return georeMachineDao.getRegionIdMachine(regionId);
    }

    @Override
    public List<Map<String, Object>> getFault(List<Integer> regionId) {
        return georeMachineDao.getFault(StringUtil.getListString(regionId));
    }

    @Override
    public List<Map<String, Object>> getBorrow(List<Integer> regionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("time", DateTimeUtil.getDayAgoTime(3));
        return georeMachineDao.getBorrow(map);
    }

    @Override
    public List<Map<String, Object>> getAbnormal(List<Integer> regionId) {
        return georeMachineDao.getAbnormal(StringUtil.getListString(regionId));
    }

    @Override
    public List<MachineAbnormal> getRegionIdAbnormal(Integer regionId) {
        return georeMachineDao.getRegionIdAbnormal(regionId);
    }

    @Override
    public List<Map<String, Object>> getPower(List<Integer> regionId, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", StringUtil.getListString(regionId));
        map.put(DaoParamConstant.accountId, accountId);
        return georeMachineDao.getPower(map);
    }

    @Override
    public List<MachineStatus> getRegionIdPower(Integer regionId, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", regionId);
        map.put(DaoParamConstant.accountId, accountId);
        return georeMachineDao.getRegionIdPower(map);
    }

    @Override
    public void deleteStatus(List<Integer> machineId) {
        List<List<Integer>> machineList = ListUtils.partition(machineId, 900);
        for (List<Integer> machines : machineList) {
            georeMachineDao.deleteStatus(StringUtil.getListString(machines));
        }
    }

    @Override
    public List<Integer> getNotStatus() {
        return georeMachineDao.getNotStatus();
    }

    @Override
    public List<Integer> getNotRegionId() {
        return georeMachineDao.getNotRegionId();
    }

    @Override
    public void deleteRegionId(List<Integer> regionIds) {
        georeMachineDao.deleteRegionId(StringUtil.getListString(regionIds));
    }

    @Override
    public List<Map<String, Object>> getAll() {
        return georeMachineDao.getAll();
    }
}
